<?php
class HeroesController extends AppController {
	public $name = 'Heroes';
	public $uses = array('Hero','Article','Profile');
	public $components = array('Webservice.Webservice','RequestHandler');
	public $viewClass = 'Webservice.Webservice';
	
	function getAllHeroes(){
		$heroes = $this->Hero->getListHeroes();
		if(isset($heroes)){
			$dataOutput->massage = "successfully";
			$dataOutput->success = true;
			$dataOutput->response = $heroes;
		}else{
			$dataOutput->massage = 'no database';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	function getHeroById($id = null){
		if(isset($id)){
			$hero = $this->Hero->getHeroById($id);
			if(isset($hero)){
				$dataOutput->massage = "successfully";
				$dataOutput->success = true;
				$profile = $this->Profile->getProfileByHeroId($id);
				if(!empty($profile)){
					$hero[0]['Hero']['description']=$profile['Profile']['description'];
				}
				$dataOutput->response = $hero;
				$arrArticles = $this->Article->getListArticlesByHeroId($id);
				if(!empty($arrArticles)){
					$i=0;
					foreach($arrArticles as $article){
						$articles[$i]['Article']['id'] = $article['Article']['id'];
						$articles[$i]['Article']['hero_id'] = $article['Article']['hero_id'];
						$articles[$i]['Article']['title'] = $article['Article']['title'];
						$articles[$i]['Article']['number_of_read'] = $article['Article']['number_of_read'];
						$i++;
					}
					$dataOutput->articles = $articles;
				}else{
					$dataOutput->articles = 'no database';
				}
			}else{
				$dataOutput->massage = 'no database';
				$dataOutput->success = false;
			}
		}else{
			$dataOutput->massage = 'please! input hero id';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	
	function getHeroesByPeriodId($id = null){
		if(isset($id)){
			$heroes = $this->Hero->getHeroesByPeriodId($id);
			if(isset($heroes)){
				$dataOutput->massage = "successfully";
				$dataOutput->success = true;
				$dataOutput->response = $heroes;
			}else{
				$dataOutput->massage = 'no database';
				$dataOutput->success = false;
			}
		}else{
			$dataOutput->massage = 'please! input period id';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	
	function getHeroesByName($name = null){
		if(isset($name)){
			$heroes = $this->Hero->getHeroesByName($name);
		}else{
			$heroes = $this->Hero->getListHeroes();
		}
		if(!empty($heroes)){
			$dataOutput->massage = "successfully";
			$dataOutput->success = true;
			$dataOutput->response = $heroes;
		}else{
			$dataOutput->massage = 'no database';
			$dataOutput->success = false;
		}
		$this->set(compact('dataOutput'));
	}
	
	
}